package ch08
{
	import flash.display.Sprite;
	import flash.events.Event;

	import utils.Ball;

	/**
	 * 鼠标跟随弹性运动.
	 * @author sjp
	 *
	 */
	public class SpringToMouse extends Sprite
	{
		//弹性
		private var spring:Number=0.1;
		//摩擦力
		private var friction:Number=0.95;
		private var ball:Ball;
		private var vx:Number=0;
		private var vy:Number=0;


		public function SpringToMouse()
		{
			init()
		}

		private function init():void
		{
			ball=new Ball();
			addChild(ball);
			this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function enterFrameHandler(event:Event):void
		{
			var ax:Number=(mouseX - ball.x) * spring;
			var ay:Number=(mouseY - ball.y) * spring;
			vx+=ax;
			vy+=ay
			vx*=friction;
			vy*=friction;
			ball.x+=vx;
			ball.y+=vy;
		}
	}
}